@file:OptIn(ExperimentalAnimationApi::class)

package com.zykj.evaluation

import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.animation.AnimatedVisibilityScope
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.SideEffect
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.viewinterop.AndroidViewBinding
import androidx.core.view.WindowCompat
import androidx.navigation.NavBackStackEntry
import androidx.navigation.NavController
import androidx.navigation.NavGraphBuilder
import androidx.navigation.fragment.NavHostFragment
import com.google.accompanist.navigation.animation.composable
import com.google.accompanist.systemuicontroller.rememberSystemUiController
import com.zykj.evaluation.databinding.ContentMainBinding
import com.zykj.evaluation.ui.theme.EvaluationTheme
import dagger.hilt.android.AndroidEntryPoint


@AndroidEntryPoint
class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //状态栏与内容区域合为一体
        WindowCompat.setDecorFitsSystemWindows(window, false)

        setContent {
            EvaluationTheme {
                val systemUiController = rememberSystemUiController()
                val inDarkTheme = isSystemInDarkTheme()
                SideEffect {
                    //沉浸式状态栏
                    systemUiController.setStatusBarColor(
                        Color.Transparent,
                        darkIcons = !inDarkTheme
                    )
                }
                AndroidViewBinding(ContentMainBinding::inflate)
            }
        }
    }

    override fun onSupportNavigateUp(): Boolean {
        return findNavController().navigateUp() || super.onSupportNavigateUp()
    }

    private fun findNavController(): NavController {
        val navHostFragment =
            supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
        return navHostFragment.navController
    }
}

private fun NavGraphBuilder.animatedComposable(
    route: String,
    content: @Composable AnimatedVisibilityScope.(NavBackStackEntry) -> Unit
) {
    composable(
        route = route
    ) { backStackEntry ->
        content(backStackEntry)
    }
}



